import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/core/constans/dict_data_info.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_entities_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/get_dict_all_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/double_control/get_user_by_id_entity.dart';

class PeopleInformationListController extends GetxController {
  /// 搜索使用key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  /// 筛选右侧栏控制Key
  final popRightScaffoldKey = GlobalKey<ScaffoldState>();

  /// 筛选列表
  List<FilterBean> filterList = [];

  /// 搜索条件
  String? search;

  /// 选中的学历
  String? education;

  /// 选中的性别
  int? sex;

  @override
  void onInit() {
    super.onInit();
    initData();
  }

  /// 初始化数据
  Future<void> initData() async {
    // 获取类型字典
    final List<GetDictAllEntity> educationDictList = await DictDataInfo.getDictionaryDataByTree(dictionaryType: 'qyryxllx_qhWGTfBV');

    final List<FilterEntitiesBean> educationList = [];

    // 添加学历筛选条件
    for (var item in educationDictList) {
      educationList.add(FilterEntitiesBean(name: item.dictionaryLabel!, value: item.id!));
    }

    final List<FilterEntitiesBean> sexList = [];

    // 添加性别筛选条件
    sexList.add(FilterEntitiesBean(name: '男', value: 0));
    sexList.add(FilterEntitiesBean(name: '女', value: 1));
    sexList.add(FilterEntitiesBean(name: '保密', value: 2));

    //筛选控制bean
    final FilterBean filterBean = FilterBean(title: '学历', filterList: educationList);
    final FilterBean filterBean3 = FilterBean(title: '性别', filterList: sexList);
    filterList.add(filterBean);
    filterList.add(filterBean3);
  }

  // 获取列表数据
  Future<List<GetUserByIdEntity>> getListData(int pageNum, int pageSize) async {
    // 获取列表接口
    final res = await HttpUtil().generateGetRequest<List<GetUserByIdEntity>>(
      Interface.getUserPageList,
      queryParameters: {
        'pageNum': pageNum,
        'pageSize': pageSize,
        'keyWords': search,
        'education': education,
        'sex': sex,
      },
    ).execute();
    List<GetUserByIdEntity>? data;
    res.onSuccess((result) {
      if (result.data != null) {
        data = result.data!;
      }
    });
    return data ?? [];
  }

  /// 筛选确认
  void screenConfirm(List<FilterBean> value) {
    filterList = value;
    education = null;
    sex = null;
    for (var i in value) {
      if (i.chosenValue.isEmpty) {
        continue;
      }
      if (i.title == '学历') {
        education = i.chosenValue[0].value as String;
      }
      if (i.title == '性别') {
        sex = i.chosenValue[0].value as int;
      }
    }
    update();
    refreshPage();
  }

  /// 筛选
  void screen() {
    popRightScaffoldKey.currentState?.openEndDrawer();
  }

  /// 搜索
  void refreshPage() {
    globalKey.currentState?.onRefresh();
  }
}
